<?php

namespace app\admin\model;

use think\Model;

class Access extends Model
{
    public $table = 'access';
    public $auth = 'auth';
    public $auth_access = 'auth_access';
    public $admin = 'admin';

    /**
     * 获取第一级菜单
     */
    public function getParentMenu($type = 1)
    {
        return $this->field('id,name')
            ->where('status', 1)
            ->where('pid', 0)
            ->where('lang', $type)->select();
    }

    /**
     * 获取自己的权限
     * @param number $groupId
     * @param string $field
     * @param number $lang
     */
    public function getMyAccess($groupId = 0, $field = '')
    {
        static $access = [];
        $field = empty($field) ? 'id,pid,name,menu,menu_icon,url,key' : $field;
        if (empty($access[$groupId])) {
            $access[$groupId] = [];
            $where = ['status' => 1];
            if ($groupId == 0) {
                $access[$groupId] = $this->field($field)->where($where)->order('id asc')->select();
            } else {
                //根据用户组查询
                $where['auth_id'] = $groupId;
                $access[$groupId] = $this->table($this->auth_access)->alias('as')->join($this->table . ' a', 'as.access_id = a.id AND a.status = 1')
                    ->field($field)->where($where)->order('id asc')->select();
            }
        }
        return $access[$groupId];
    }

    public function getAccess($lang = 1)
    {
        return $this->table($this->table)->where([['status', '=', 1], ['lang', '=', $lang]])->select();
    }

    public function getAuthList()
    {
        $auth = $this->getAuthLists();
        foreach ($auth as &$item) {
            $item['access'] = $this->getMyAccess($item['id'], 'id', 0);
            $item['person_num'] = $this->getAuthPersonNum($item['id']);
        }
        return $auth;
    }

    public function getAuthLists()
    {
        return $this->table($this->auth)->where('status', '<>', -1)->select();
    }


    public function getAuth($authId)
    {
        return $this->table($this->auth)->where(['id' => $authId])->find();
    }

    public function saveAuth($data)
    {
        $this->table = $this->auth;
        if ($data['id'] > 0) {
            $this->table('auth')->update($data);
            return $data['id'];
        } else {
            $this->table('auth')->save($data);
            return $this->getLastInsID();
        }
    }

    public function saveAuthRole($authId, $accessId = [])
    {
        $data = [];
        foreach ($accessId as $id) {
            $data[] = ['access_id' => $id, 'auth_id' => $authId];
        }
        $result = $this->table($this->auth_access)->where(['auth_id' => $authId])->delete();
        if ($result !== false) {
            $this->autoWriteTimestamp = false;
            $result = $this->table($this->auth_access)->insertAll($data);
        }
        if ($result !== false) {
            return true;
        }
        return false;
    }

    public function delAuth($id)
    {
        $exist = $this->table($this->admin)
            ->where('auth_id', $id)->where('status', '<>', -1)
            ->find();
        if ($exist)
            return error('还有员工存在，不能删除');
        $result = $this->table($this->auth)->where(['id' => $id])->update(['status' => -1]);
        if ($result)
            return success('');
        return error('删除失败');
    }

    public function getAuthPersonNum($id)
    {
        return $this->table($this->admin)->where('auth_id', $id)->where('status', '<>', -1)->count();
    }

    public function getRuleEmployee($where = [], $page = 1, $pageSize = 15)
    {
        $where[] = ['status', '<>', -1];
        $tables = $this->table($this->admin)->where($where)->field('id , name,true_name ,create_time')->buildSql();
        return $this->pageResult($tables, $page, $pageSize);
    }


}

